<?php
session_start();
$user_session = session_id();
$user_id = 1;

$delta = 3600;
$now_time = time() + 25200;
$now_time_delta = $now_time - $delta;

global $wpdb;
/* Cập nhật user mới online */
$shnt_query_string = "SELECT count(id) as count_user_online FROM shnt_user_online WHERE user_session = '" . $user_session . "'";
$shnt_user_online = $wpdb->get_results($shnt_query_string);

/* Nếu user đã truy cập trước đó thì cập nhật lại giờ */
if ($shnt_user_online[0]->count_user_online > 0) {
   $shnt_query_string = "UPDATE shnt_user_online SET user_id = $user_id, ";
   $shnt_query_string .= "user_time = $now_time ";
   $shnt_query_string .= "WHERE user_session = '" . $user_session . "'";
}
/* Thêm mới */ else {
   $shnt_query_string = "INSERT INTO shnt_user_online(user_id, user_session, user_time) VALUE($user_id, '$user_session', $now_time)";
}

$shnt_insert_result = $wpdb->get_results($shnt_query_string);
/* Xóa những user cũ */
$shnt_query_string = "DELETE FROM shnt_user_online WHERE user_time < $now_time_delta";
$shnt_delete_result = $wpdb->get_results($shnt_query_string);

/* Đếm số người online */
$shnt_query_string = "SELECT count(id) as count_user_online FROM shnt_user_online";

$count_user_online = $wpdb->get_results($shnt_query_string);
$count_user_online = $count_user_online[0]->count_user_online;
$_SESSION['count_user_online'] = $count_user_online;
?>
